<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .w {
            width: 1226px;
            margin: 0 auto;
        }

        .header {
            height: 150px;
            background: grey;
        }

        .wrapNav {
            height: 45px;
        }

        .navBar {
            background-color: #ff0;
            width: 100%;
        }
        .fixed{
            position: fixed;
            left: 0;
            top: 0;
        }

        .nav {
            display: flex;
            height: 45px;
            line-height: 45px;
        }

        .nav li {
            flex: 1;
            border: 1px solid #000;
            text-align: center;
        }

        .nav li+li {
            border-left: 0;
        }

        .main .part {
            height: 400px;
            text-align: center;
            line-height: 400px;
            border: 1px solid #000;
        }

        .main .part+.part {
            border-top: 0;
        }

        .tips {
            width: 100%;
            line-height: 45px;
            position: fixed;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            text-align: center;
            display: none;
        }

        .back {
            width: 45px;
            height: 45px;
            border: 1px solid #333;
            text-align: center;
            line-height: 45px;
            position: fixed;
            right: 5px;
            bottom: 50px;
            display: none;
        }
    </style>
</head>

<body>
    <div class="header w"></div>
    <div class="wrapNav">
        <div class="navBar">
            <ul class="nav w">
                <li><a href="#one">link_01</a></li>
                <li>link_02</li>
                <li>link_03</li>
                <li>link_04</li>
                <li>link_05</li>
                <li>link_06</li>
            </ul>
        </div>
    </div>
    <div class="main w">
        <div id="one" class="part">part_01</div>
        <div class="part">part_02</div>
        <div class="part">part_03</div>
        <div class="part">part_04</div>
        <div class="part">part_05</div>
        <div class="part">part_06</div>
        <div class="part">part_07</div>
        <div class="part">part_08</div>
        <div class="part">part_09</div>
        <div class="part">part_10</div>
    </div>
    <div class="tips">到达底部</div>
    <div class="back">top</div>
</body>
<script>

    var header = document.getElementsByClassName("header")[0];
    var navBar = document.getElementsByClassName("navBar")[0];
    var tips = document.getElementsByClassName("tips")[0];
    var back = document.getElementsByClassName("back")[0];
    var headerHeight = header.clientHeight;
    window.onscroll = function(){
        // 滚动条的当前位置
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        // console.log(scrollTop);

        // 导航栏吸顶
        if(scrollTop >= headerHeight){
            // navBar.style.position = "fixed";
            // navBar.style.top = "0";
            // navBar.style.left = "0";

            navBar.classList.add("fixed");
        }else{
            // navBar.style.position = "static";
            navBar.classList.remove("fixed");
        }

        var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;

        var scrollMax = scrollHeight - clientHeight;
        console.log(scrollMax,scrollTop);

        // 理想情况下  scrollTop == scrollMax  但是(由于页面缩放1.25 =>滚动条可能出出现小数 => 误差)
        // 怎么解决?   => 设置误差允许范围   [scrollMax-1,scrollMax+1]
        // scrollTop >= scrollMax-1 && scrollTop <= scrollMax+1
        // Math.abs(scrollMax-scrollTop)<=1

        if(scrollTop >= scrollMax-1 && scrollTop <= scrollMax+1){
            console.log("滚动条到达底部");
            back.style.display = "block";
            tips.style.display = "block";
            setTimeout(function(){
                tips.style.display = "none";
            },500);
        }
        // if(Math.abs(scrollMax-scrollTop)<=1){
        //     console.log("滚动条到达底部");
        // }


    }
    back.onclick = function(){
        // document.documentElement.scrollTop = 0;
        
        // window.scrollTo(0,0);
        window.scrollTo({
            top:0,
            behavior:"smooth"
        });
    }


</script>
</html>